package com.shizhanzhe.gxb.home.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.shizhanzhe.gxb.R;
import com.shizhanzhe.gxb.base.UserBean;
import com.shizhanzhe.gxb.home.bean.GoodsListBean;
import com.shizhanzhe.gxb.utils.Util;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by YJ on 2018/10/23.
 */
public class MallGoodsAdapter extends RecyclerView.Adapter<MallGoodsAdapter.ViewHolder> {

    private Context mContext;
    private List<GoodsListBean> mData = new ArrayList<>();
    private MallGoodsListener listener;

    public MallGoodsAdapter(Context context) {
        mContext = context;
    }

    public void setListener(MallGoodsListener listener) {
        this.listener = listener;
    }

    public void updateData(int page,List<GoodsListBean> dataSet) {
        if (dataSet != null && !dataSet.isEmpty()) {
            if (page==0) {
                mData = dataSet;
            } else {
                mData.addAll(dataSet);
            }
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_recommend, parent, false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        final GoodsListBean bean = mData.get(position);
//        Glide.with(mContext).load(dataBean.getGoods_thumbnail_url()).apply(new RequestOptions().error(R.mipmap.logo)).into(holder.reImg);
//        holder.tvReName.setText(dataBean.getGoods_name());
//        holder.tvRePrice.setText((double) dataBean.getMin_group_price() / 100 + "元");
//        holder.itemView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                listener.click(dataBean);
//            }
//        });
        Glide.with(mContext).load(bean.getGoods_thumbnail_url()).apply(new RequestOptions().error(R.mipmap.load_img_error)).into(holder.ivLogo);
        holder.tvTitle.setText(bean.getGoods_name());
        double price=bean.getMin_group_price()/100;
        double qhprice=((double) bean.getMin_group_price()-(double)bean.getCoupon_discount())/100;
        String yj= Util.Sub(qhprice*((double)bean.getPromotion_rate()/1000)* UserBean.vipdivide+"");
        String yj2= Util.Sub(qhprice*((double)bean.getPromotion_rate()/1000)* UserBean.divide+"");
        holder.tvQuanHouPrice.setText("¥"+qhprice);
        holder.tvPriceOnLine.setText("¥"+price);
        holder.tvPriceOnLine.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG );
        holder.tvYongPrice.setText("预计赚¥"+yj2);
        holder.tvUpPrice.setText("升级赚¥"+yj);
        holder.tvSellCount.setText("月销量:"+bean.getSold_quantity());
        holder.quan.setText(bean.getCoupon_discount()/100+"元券");
        if (bean.getCoupon_discount() / 100==0){
            holder.quan.setVisibility(View.GONE);
            holder.tvPriceOnLine.setVisibility(View.GONE);
        } else {
            holder.quan.setVisibility(View.VISIBLE);
            holder.tvPriceOnLine.setVisibility(View.VISIBLE);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.click(bean);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_logo)
        ImageView ivLogo;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.tv_quan_hou_price)
        TextView tvQuanHouPrice;
        @BindView(R.id.tv_price_on_line)
        TextView tvPriceOnLine;
        @BindView(R.id.quan)
        TextView quan;
        @BindView(R.id.tv_sell_count)
        TextView tvSellCount;
        @BindView(R.id.tv_yong_price)
        TextView tvYongPrice;
        @BindView(R.id.tv_up_price)
        TextView tvUpPrice;
        @BindView(R.id.ll_detail)
        LinearLayout llDetail;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface MallGoodsListener {
        void click(GoodsListBean item);
    }
}
